//////////////////////////////////////////////////////////////////////////////////////////////
//	File			:	"Interface.cpp"
//
//	Author			:	Mike Davis, Matt Stover, James Thornton, Keith Webster
//	
//	Project			:	TacWars
//
//	Last Modified	:	11/6/2013
//
//	Purpose			:	
//
//////////////////////////////////////////////////////////////////////////////////////////////

#include "Interface.h"
#include "GameObjects\Cursor.h"
#include "GameObjects\Player.h"
#include "MapSystem\MapManager.h"
#include "MapSystem\BattleMap.h"
#include "StateMachine\GameplayState.h"
#include "GameObjects\Entity.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/SGD_String.h"


Interface::Interface()
{
	player1 = player2 = nullptr;
	cursor = GameplayState::GetInstance()->GetCursor();
	player1 = GameplayState::GetInstance()->player1;
	player2 = GameplayState::GetInstance()->player2;
	map = MapManager::GetInstance()->GetMap();

	selected = target = nullptr;
	
	
}

Interface::~Interface()
{

}

void Interface::Update(float dt)
{
	if(selected != GameplayState::GetInstance()->selected)
	selected = GameplayState::GetInstance()->selected;

	//If in select target/move state
	target = map->GetMap()[Index(cursor->GetPosX() / 64,cursor->GetPosY()/64)].element->OccupyingUnit;

	
}

void Interface::Render()
{
	if(selected)
	{
		//Render selected unit info
		RECT selectionRect = {10,30,225,110};
		CSGD_Direct3D::GetInstance()->DrawRect(selectionRect,D3DCOLOR_ARGB(255,255,255,255));
		CSGD_Direct3D::GetInstance()->DrawHollowRect(selectionRect, D3DCOLOR_ARGB(255,255,0,0));
		CSGD_Direct3D::GetInstance()->DrawText(_T("Selected Unit"),10,5, D3DCOLOR_ARGB(255,0,0,0));

		TOSTRINGSTREAM toss;

		switch(selected->GetType())
		{
		case 0:	//General
			toss << _T("General");
			break;
		case 1:	//Berserker
			toss << _T("Berserker");
			break;
		case 2:	//Knight
			toss << _T("Knight");
			break;
		case 3:	//Priest
			toss << _T("Priest");
			break;
		case 4:	//Ranger
			toss << _T("Ranger");
			break;
		}

		CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 12, 31, D3DCOLOR_ARGB(255,0,0,0));
		toss.str(_T(""));
		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->heartID,
			12,55, 0.075f,0.075f);
		toss << selected->GetHealth();
		CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 30, 55, D3DCOLOR_ARGB(255,0,0,0));
		toss.str(_T(""));

		if(selected->GetType() != 0)
		{
			CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->crownID,
				62,55, 0.075f,0.075f);
			toss << selected->GetLoyalty();
			CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 83, 55, D3DCOLOR_ARGB(255,0,0,0));
			toss.str(_T(""));
		}

		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->swordID,
			12,80, 0.015f,0.015f);
		toss << selected->GetAttackDamage();
		CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 30, 80, D3DCOLOR_ARGB(255,0,0,0));
		toss.str(_T(""));

		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->bootID,
			62,80, 0.04f,0.04f);
		toss << selected->GetMoveRange();
		CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 83, 80, D3DCOLOR_ARGB(255,0,0,0));
		toss.str(_T(""));

		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->bootID,
			102,80, 0.04f,0.04f);
		toss << selected->GetAttackRange();
		CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 123, 80, D3DCOLOR_ARGB(255,0,0,0));
		toss.str(_T(""));

		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->portraitID,
			150, 32, 2.3,2.3);
		
	}
	if(target)
	{
		RECT targetRect = {575,30,790,110};	
		CSGD_Direct3D::GetInstance()->DrawRect(targetRect,D3DCOLOR_ARGB(255,255,255,255));	
		CSGD_Direct3D::GetInstance()->DrawHollowRect(targetRect, D3DCOLOR_ARGB(255,255,0,0));	
		CSGD_Direct3D::GetInstance()->DrawText(_T("Target Unit"),575,5, D3DCOLOR_ARGB(255,0,0,0));

		TOSTRINGSTREAM toss;

		switch(target->GetType())
		{
		case 0:	//General
			toss << _T("General");
			break;
		case 1:	//Berserker
			toss << _T("Berserker");
			break;
		case 2:	//Knight
			toss << _T("Knight");
			break;
		case 3:	//Priest
			toss << _T("Priest");
			break;
		case 4:	//Ranger
			toss << _T("Ranger");
			break;
		}

		CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 577, 31, D3DCOLOR_ARGB(255,0,0,0));
		toss.str(_T(""));
		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->heartID,
			577,55, 0.075f,0.075f);
		toss << target->GetHealth();
		CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 597, 55, D3DCOLOR_ARGB(255,0,0,0));
		toss.str(_T(""));

		if(target->GetType() != 0)
		{
			CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->crownID,
				627,55, 0.075f,0.075f);
			toss << target->GetLoyalty();
			CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 650, 55, D3DCOLOR_ARGB(255,0,0,0));
			toss.str(_T(""));
		}

		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->swordID,
			577,80, 0.015f,0.015f);
		toss << target->GetAttackDamage();
		CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 597, 80, D3DCOLOR_ARGB(255,0,0,0));
		toss.str(_T(""));

		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->bootID,
			627,80, 0.04f,0.04f);
		toss << target->GetMoveRange();
		CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 650, 80, D3DCOLOR_ARGB(255,0,0,0));
		toss.str(_T(""));

		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->bootID,
			667,80, 0.04f,0.04f);
		toss << target->GetAttackRange();
		CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 690, 80, D3DCOLOR_ARGB(255,0,0,0));
		toss.str(_T(""));

		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->portraitID,
			715, 32, 2.3,2.3);
	}

	for (int i = 0; i < player1->Squad.size(); i++)
	{
		D3DCOLOR box = D3DCOLOR_ARGB(255,0,0,255);
		if(player1->Squad[i]==selected)
			box = D3DCOLOR_ARGB(255,255,255,0);
		if( player1->Squad[i] == target)
			box = D3DCOLOR_ARGB(255,255,0,0);

		RECT infoRect = {-1, (45 *i)+130, 150, (45 *(i+1))+120};
		CSGD_Direct3D::GetInstance()->DrawRect(infoRect, D3DCOLOR_ARGB(255,255,255,255));
		CSGD_Direct3D::GetInstance()->DrawHollowRect(infoRect, box);
		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->portraitID,
			1,(45*i)+131, 1,1);
		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->heartID,
			50,(45*i)+131, .07,.07);
		
		RECT hRect = { 70, (45*i) + 136, 145, (45*i) + 142};
		CSGD_Direct3D::GetInstance()->DrawRect(hRect, D3DCOLOR_ARGB(255,100,100,100));
		hRect.right = (float(hRect.right - hRect.left) * float(player1->Squad[i]->GetHealth() / (float)player1->Squad[i]->GetMaxHP())) + hRect.left;
		CSGD_Direct3D::GetInstance()->DrawRect(hRect, D3DCOLOR_ARGB(255,255,0,0));
		if(player1->Squad[i]->GetType() !=0)
		{
			CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->crownID,
			50,(45*i)+151, .07,.07);
			RECT lRect = { 70, (45*i) + 156, 145, (45*i) + 162};
			CSGD_Direct3D::GetInstance()->DrawRect(lRect, D3DCOLOR_ARGB(255,100,100,100));
			lRect.right = (float(lRect.right - lRect.left) * float(player1->Squad[i]->GetLoyalty() / (float)player1->Squad[i]->GetMaxLP())) + lRect.left;
			CSGD_Direct3D::GetInstance()->DrawRect(lRect, D3DCOLOR_ARGB(255,0,0,255));
		}
	}

	for (int i = 0; player2 && i < player2->Squad.size(); i++)
	{
		D3DCOLOR box = D3DCOLOR_ARGB(255,0,0,255);
		if(player2->Squad[i]==selected)
			box = D3DCOLOR_ARGB(255,255,255,0);
		if( player2->Squad[i] == target)
			box = D3DCOLOR_ARGB(255,255,0,0);

		RECT infoRect = {650, (45 *i)+130, 801, (45 *(i+1))+120};
		CSGD_Direct3D::GetInstance()->DrawRect(infoRect, D3DCOLOR_ARGB(255,255,255,255));
		CSGD_Direct3D::GetInstance()->DrawHollowRect(infoRect, box);
		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->portraitID,
			652,(45*i)+131, 1,1);
		CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->heartID,
			701,(45*i)+131, .07,.07);
		
		RECT hRect = { 721, (45*i) + 136, 796, (45*i) + 142};
		CSGD_Direct3D::GetInstance()->DrawRect(hRect, D3DCOLOR_ARGB(255,100,100,100));
		hRect.right = (float(hRect.right - hRect.left) * float(player2->Squad[i]->GetHealth() / (float)player2->Squad[i]->GetMaxHP())) + hRect.left;
		CSGD_Direct3D::GetInstance()->DrawRect(hRect, D3DCOLOR_ARGB(255,255,0,0));
		if(player2->Squad[i]->GetType() !=0)
		{
			CSGD_TextureManager::GetInstance()->Draw(GameplayState::GetInstance()->crownID,
			701,(45*i)+151, .07,.07);
			RECT lRect = { 721, (45*i) + 156, 796, (45*i) + 162};
			CSGD_Direct3D::GetInstance()->DrawRect(lRect, D3DCOLOR_ARGB(255,100,100,100));
			lRect.right = (float(lRect.right - lRect.left) * float(player2->Squad[i]->GetLoyalty() / (float)player2->Squad[i]->GetMaxLP())) + lRect.left;
			CSGD_Direct3D::GetInstance()->DrawRect(lRect, D3DCOLOR_ARGB(255,0,0,255));
		}
	}

	RECT turnRect = {300, 10, 500, 70};
	CSGD_Direct3D::GetInstance()->DrawRect(turnRect, D3DCOLOR_ARGB(255,255,255,255));
	CSGD_Direct3D::GetInstance()->DrawHollowRect(turnRect, D3DCOLOR_ARGB(255,0,255,0));

	TOSTRINGSTREAM toss;
	toss << _T("Player ");

	if(GameplayState::GetInstance()->GetTurnCount() %2 == 0)
		toss << _T("1");
	else
		toss << _T("2");

	toss << _T("'s");

	CSGD_Direct3D::GetInstance()->DrawText(toss.str().c_str(), 345, 15, D3DCOLOR_ARGB(255,0,0,0));
	CSGD_Direct3D::GetInstance()->DrawText(_T("Turn"), 375, 45, D3DCOLOR_ARGB(255,0,0,0));



}

int Interface::Index(int x, int y)
{
	//return (y*(map->GetWidth()) + x);
	return y*22 + x;
}



